<?php
/**
 * Project: Vietsam
 * Package Controller
 * Class: Register
 * Author: Hung Nguyen		email: quochungndvt88@yahoo.com
 * Created on Fer 8, 2012
 * Updated on Fer 30, 2012
 *
 * */
if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Orders extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->library('parser');
		$this->load->helper(array('form', 'url'));
		$this->load->helper('date');
		$this->load->library('email');
		$this->load->model('customer_model','customer');
		$this->load->model('order_model','order');
		//$this->template->add_region('new_tours');
		$this->buildNavigation();
		$this->buildAccountBox();

		//$this->buildSearchByConditionBox();
		$this->buildSupportBox();
		//$this->buildAdvBox();
		//$this->buildSlider();
		//$this->buildCartBox();
		//$this->buildCompareBox();

	}

	function index($offset = 0) {
		if(!$this->checkUserLogin()){
			redirect("auth");
			return;
		}
		
		$this->view($offset);
	}

	function view($offset = 0){
		
		$user = $this->session->userdata('customer');
		//print_r($user);
		
		$this->load->library('pagination');
		
		$base_url = site_url ( 'member/orders/view');
        $total_rows = $this->order->countSearch($user['id_user']); 
        $per_page = 20; //NUMBER_RECORD_PER_PRODUCT_PAGE;
        $uri_segment = 4;
        
        pagination_front($base_url, $total_rows, $per_page, $uri_segment);
        
		$orders = $this->order->search($offset, $per_page, $user['id_user']);
		
		foreach ($orders as &$item){
			$item['status'] = lang( $item['order_status']);
			$item['edit_url'] = site_url('member/orders/detail/' . $item['order_id']);
		}
		
//		print_r($orders);
		$data['items'] = $orders;
		
		$this->template->parse_view('right_content2column', $this->skins.'member/order/member_order_view', $data);
		$this->template->render();
	}

	function detail($orderId){
		
		$data = array();
		
		$this->load->model('order_model','order');
		
		$items = $this->order->getItemsOrder($orderId);
		$index = 1;
		foreach ($items as &$item){
			$item['subtotal'] = $item['quantity'] * $item['product_price_actual'];
			$item['no'] = $index;
			$index++;
		}
		
		$data['items'] = $items;
		$data['order_id'] = $orderId;
		
		$this->template->parse_view('right_content2column', $this->skins.'member/order/member_order_detail_view', $data);
		$this->template->render();
	}
	
	function create(){

		$this->load->model('order_model','order');
		$this->load->model('cart_model','cart');
		$user = $this->session->userdata('customer');
		$date = date('Y-m-d H:i:s', time());
		$billing_info = $this->session->userdata('billing_information');
		$shipping_info = $this->session->userdata('shipping_information');
		$payment_info = $this->session->userdata('payment_information');

		$data = array_merge($billing_info, $shipping_info, $payment_info);

		$data['buyer_register_user_id'] = $user['id_user'];
		$data['seller_company_id'] = 1;
		$data['created_date'] = $date;
		$data['expected_deliver_date'] = $date;
		$data['actual_deliver_date'] = $date;
		$data['order_total_amount'] = 0;
		$data['order_status'] = 1;
		$data['process_register_user_id'] = 1;
		$data['s_firstname'] = $shipping_info['s_firstname'];
		$data['s_lastname'] = $shipping_info['s_lastname'];
		$data['s_address'] = $shipping_info['s_address'];
		$data['s_phone'] = $shipping_info['s_phone'];
		$data['s_note'] = '';//$shipping_info['s_note'];
		$data['s_city'] = $shipping_info['s_city'];
		$data['s_state'] = $shipping_info['s_state'];
		$data['s_country'] = $shipping_info['s_country'];
		$data['shipping'] = $shipping_info['shipping_method'];
		$data['shipping_fee'] = 0;
		$data['last_process_date'] = $date;
		$data['payment'] = $payment_info['payment_method'];
		$data['card_type'] = '';//$shipping_info[''];
		$data['card_account_number'] = '';//$shipping_info[''];
		$data['card_expire_date'] = '';//$shipping_info[''];
		$data['tx'] = '';
		$data['tax'] = 0;
		$data['payment_status'] = 0;

		$member_id = $this->session->userdata('member_id');
		$data['cart'] = $this->cart->getCartMember( $member_id);
		$data['site_url'] = site_url();

		//print_r($data);
		$orderId = $this->order->insert($data);
		//return;
		// if (empty($data['products']))
		// 	echo "Shopping cart is empty.";

		//print_r($data['products']);

		$lang = getLangCode();

		$totalItem = 0;

		foreach ($data['cart'] as &$item){
			$item['name'] = $item[ $lang . '_name'];
			$item['img'] = img(array('src' => getDirByDate($item['date_add'], PRODUCT_IMAGE_PATH) . '/' . $item['image'],  'height' => '60', 'width' => '60' ));
			$item['delete'] = anchor('cart/delete/' . $item['product_id'], 'Delete', 'title="Delete '. $item['name'] .'"');
			$item['subtotal'] = $item['price'] * $item['quantity'] ;
			$item['order_id'] = $orderId;
			$item['product_input'] = form_hidden( 'product_id[]' , $item['product_id']);
			$totalItem += $item['subtotal'];
			$item['product_price_actual'] = $item['price'];
			$item['product_price_paid'] = $item['price'];
			$totalItem += $item['subtotal'];
			$this->order->insertDetail($item);
		}


		// send email to buyer:
		$data['date'] = $data['created_date'];
		$data['total_item'] = $totalItem;
		$data['total'] = $totalItem + $data['shipping_fee'];


		$data['s_country_name'] = getCountry($data['s_country']);
			
		$data['b_country_name'] = getCountry($data['b_country']);
		//		print_r($data);
		//		echo "sssdsdsd: " . $data['b_country'];
		//		return;

		$data['order_id'] = $orderId;
		//echo $this->load->view('default/front_end/order/view_email_order', $data, true);
		//echo $this->parser->parse('default/front_end/order/view_email_order', $data, true);



		$from = "sale@angelbeautycaly.com <Angel Beauty Caly>";
		$to = $user['email'];
		$subject = 'Order AngelbeautyCaly #' . $orderId;
		$message = $this->parser->parse('default/front_end/order/view_email_order', $data, true);
		sendMail($from, $to, $subject, $message);


		$this->load->library('encrypt');

		$custom = $this->encrypt->encode($data['buyer_register_user_id'] . '||' . $data['created_date'] . '||' . $orderId);
		$array = array('error' => false, 'message' => 'Order sucess', 'orderId' => $orderId, 'custom' => $custom);

		echo json_encode($array);
	}

	function success(){
		$this->load->library('encrypt');

		$tx = $this->input->post('tx');
		$st = $this->input->post('st');  // Completed
		$cc = $this->input->post('cc');
		$cm = $this->input->post('cm');
		$amt = $this->input->post('amt');

		$custom = $this->encrypt->decode($cm);  // userId-created_date-orderid

		$data = explode('||', $custom);

		if(count($data) == 3){
			$userId = $data[0];
			$created_date = $data[1];
			$orderId = $data[2];

			$order = $this->order->getOrderById($orderId, $userId, $created_date);

			if(!empty($order)){

				$order['payment_status'] = $st;
				$order['tx'] = $tx;
				$order['order_status'] = PAID;

				$this->order->update($order);

				$from = $this->session->userdata('customer')->email; //$this->session->userdata['billing_information']['b_email'];
				$to = $this->session->userdata('customer')->email;
				$subject = 'Pay success for order ' . $orderId;
				$message = 'Pay success.';

				sendMail($from, $to, $subject, $message);

			}
		}
		else{
				
		}

		$data['message'] = "";
		$this->template->parse_view('right_content2column', 'default/front_end/checkout/payment_information_view',$post);
		$this->template->render();
	}

	function cancel($custom){
		$this->load->library('encrypt');
		$custom = $this->encrypt->decode($cm);  // userId-created_date-orderid

		$data = explode('||', $custom);

		if(count($data) == 3){
			$userId = $data[0];
			$created_date = $data[1];
			$orderId = $data[2];

			$order = $this->order->getOrderById($orderId, $userId, $created_date);

			if(!empty($order)){

				$order['payment_status'] = $st;
				$order['tx'] = $tx;
				$order['order_status'] = PAID;

				$this->order->update($order);

				$from = $this->session->userdata('customer')->email; //$this->session->userdata['billing_information']['b_email'];
				$to = $this->session->userdata('customer')->email;
				$subject = 'Pay success for order ' . $orderId;
				$message = 'Pay success.';
				sendMail($from, $to, $subject, $message);

			}
		}
	}

	function test(){
		$data['order_success'] = lang('order_success');
		$data['thankyou_for_your_order_at_website'] = lang('thankyou_for_your_order_at_website');
		$data['your_order_code'] = lang('your_order_code');
		$data['we_will_call_you_winthin_24_hour_to_comfirm_the_order'] = lang('we_will_call_you_winthin_24_hour_to_comfirm_the_order');
		$data['ordered_date'] = lang('ordered_date');
		$data['date'] = 'ngay dat hang o day';
		$data['order_value_total'] = lang('order_value_total');
		$data['order_code'] = lang('order_code');
		$data['lb_shipping_fee'] = lang('shipping_fee');
		$data['shipping_fee'] = 988.23;
		$data['total'] = 454556;
		$data['lb_total'] = lang('total');
		$data['total_value_of_payment'] = lang('total_value_of_payment');
		$data['order_information'] = lang('order_information');
		$data['order_detail'] = lang('order_detail');
		$data['shipping_information'] = lang('shipping_informations');
		$data['shipping_method'] = 'UPS';
		$data['lb_shipping_method'] = lang('shipping_method');
		$data['receiver'] = lang('receiver');
		$data['address'] = lang('address');
		$data[''] = lang('');
		$data[''] = lang('');
		$data[''] = lang('');
		$data[''] = lang('');
		$data[''] = lang('');
		$data[''] = lang('');

		$data['order_id'] = 983434;
		//echo $this->load->view('default/front_end/order/view_email_order', $data, true);
		echo $this->parser->parse('default/front_end/order/view_email_order', $data, true);
	}


}

?>